package kontrol;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import database.DALException;
import database.MySQLReceptDAO;
import database.ReceptDTO;

/**
 * Servlet implementation class Recept
 */
@WebServlet({ "/Recept", "/Recept/*" })
public class Recept extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Recept() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		if (request.getRequestURI().endsWith("Recept/AddRecept")) {
			request.setAttribute("error", "");
			request.getRequestDispatcher("/WEB-INF/recept/createrecept.jsp")
					.forward(request, response);
		} else if (request.getRequestURI().endsWith("Recept/ShowRecept")) {
			
			formatReceptList(request);
			request.getRequestDispatcher("/WEB-INF/recept/showrecept.jsp").forward(request, response);
		
		}

		else if (request.getRequestURI().endsWith("Recept/ReceptHome")) {
			request.getRequestDispatcher("/WEB-INF/recept/recepthome.jsp")
					.forward(request, response);
		}
		return;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String data = request.getParameter("newRecept");

		if (data != null) {
			if ((request.getParameter("rNavn") != null)) {

				String receptNavn;

				receptNavn = request.getParameter("rNavn");

				MySQLReceptDAO dao;
				try {
					dao = new MySQLReceptDAO();
					dao.createRecept(receptNavn);
				} catch (DALException e1) {
					request.setAttribute("error",
							"Could not create object in database");
					e1.printStackTrace();
				}
				request.setAttribute("error", "Prescription \"" + receptNavn
						+ "\" succesfully created!");
			}
		}
		request.getRequestDispatcher("/WEB-INF/recept/createrecept.jsp")
				.forward(request, response);
	}

	public void formatReceptList(HttpServletRequest request)
			throws ServletException, IOException {

		MySQLReceptDAO receptDTO = null;
		try {
			receptDTO = new MySQLReceptDAO();
			request.setAttribute("count", receptDTO.getCount());
		} catch (DALException e) {
			e.printStackTrace();
			request.setAttribute("count", 0);
		}
		if (((Integer) request.getAttribute("count")) < 10) {
			try {
				List<ReceptDTO> liste = receptDTO.getReceptList();
				String results = "<table border=\"1\"> <tr> <th>Id </th> <th>Name</th></tr>";
				for (int i = 0; i < liste.size(); i++) {
					results += "<tr> <td>" + liste.get(i).getReceptId()
							+ "</td><td> " + liste.get(i).getReceptNavn()
							+ "<br>" + "</td> </tr>";

				}
				results += "</table>";
				request.setAttribute("results", results);
			} catch (DALException e) {

				e.printStackTrace();
			}
		} else {
			if (request.getAttribute("page") == null) {
				request.setAttribute("page", 0);
			}
			if (request.getParameter("p") != null) {
				request.setAttribute("page",
						Integer.parseInt(request.getParameter("p")));
			}
			int curPage = (Integer) request.getAttribute("page");
			try {
				List<ReceptDTO> liste = receptDTO.getReceptList(curPage, 10);
				String results = "<table border=\"1\"> <tr> <th>Id</th> <th>Name</th></tr>";
				for (int i = 0; i < liste.size(); i++) {

					results += "<tr> <td>" + liste.get(i).getReceptId()
							+ "</td><td> " + liste.get(i).getReceptNavn()
							+ "<br>" + "</td> </tr>";
				}
				results += "</table>";
				if (curPage > 9) {
					results += "<a href=\"?p=" + (curPage - 10)
							+ "\">Prev</a>&nbsp;&nbsp;&nbsp;";
				}
				if ((((Integer) request.getAttribute("count")) - 10) > curPage) {
					results += "<a href=\"?p=" + (curPage + 10) + "\">Next</a>";
				}

				request.setAttribute("results", results);
			} catch (DALException e) {

				e.printStackTrace();
			}
		}
	}

}